<template lang="">
    <div>
        <van-nav-bar title="选择量表" />
        <van-search  v-model="data.keywords" placeholder="请输入关键词"  @search="onSearch" @cancel="onSearch" />
        <div class="list">
            <div class="item" v-for="(item,index) in liangbiao">
                <div class="item-title">{{item.title}}</div>
                <div class="item-btn" @click="createOrder(item.id,item.title)">
                    发送
                </div>
            </div>
        </div>
       
        
    </div>
</template>
<script>
import { ref, reactive, onMounted } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import { getLiangBiao,createLiangBiaoOrder, getItemCate, addItemCate } from "../api/index"
import { getUserInfo, log, LOCAL_USER } from "../utils/user"
import { showToast, showConfirmDialog } from 'vant';
export default {
    setup() {
        const router = useRouter()
        const route = useRoute()
        const userInfo = getUserInfo();
        const data = reactive({
            keywords: '',
            page: 1
        })
        let pid = 0; //患者id
        const liangbiao = ref([])
        const getData = () => {
            getLiangBiao(data).then(res => {
                log("ddw", res)
                if (res.status == 'ok') {
                    liangbiao.value = res.data.list
                } else {
                    showToast(res.msg);
                }
            })
        }
        onMounted(() => {
            pid = route.params.id ? route.params.id : 0
            if(!pid){
                router.push("/Patients")
            }
            getData()
        })

        const onSearch = () => {
            data.page = 1;
            getData();
        }
        const createOrder = (id,title) => {
            showConfirmDialog({
                title: '确认发送量表',
                message:title,
            })
                .then(() => {
                    // on confirm
                    createLiangBiaoOrder({liangbiao_id:id,patient_id:pid}).then(res => {
                        if(res.status == 'ok'){
                            showToast("发送成功")
                        }else{
                            showToast(res.msg)
                        }
                    })
                })
                .catch(() => {
                    // on cancel
                });
        }
        const tab = ref('zl')
        return {
            tab,
            liangbiao,
            data,
            onSearch,
            createOrder
        }
    }
}
</script>
<style scoped>
.list {
    background: #F7F8FA;
    padding: 15px;
}

.item {
    background: #FFFFFF;
    margin-bottom: 15px;
    padding: 10px;
    display: flex;
    justify-content: space-between;
    align-items: center;
}

.item-btn {
    background: #FA541C;
    color: #FFFFFF;
    padding: 6px;
    font-size: 12px;
}
</style>